//
//  AlertDialog.m
//  PigMarket
//
//  Created by Felix on 2019/3/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AlertDialog.h"
#import "AlertDialogView.h"
/*********************************** AlertDialog *************************************/

@interface AlertDialog ()

/**
 alertDialogView
 */
@property (nonatomic , strong) AlertDialogView *alertDialogView;

/**
 builder
 */
@property (nonatomic , strong) AlertDialogBuilder *builder;

@end

@implementation AlertDialog

#pragma mark - init method
- (instancetype)initWithAlertDialogBuilder:(AlertDialogBuilder *)builder {
    
    self.builder = builder;
    
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (UIView *)getContentView {
    
    return self.alertDialogView;
}

- (void)setBuilder:(AlertDialogBuilder *)builder {
    _builder = builder;
    if (builder.title) {
        self.alertDialogView.titleLabel.text = builder.title;
    }
    if (builder.contentText) {
        self.alertDialogView.contentLabel.text = builder.contentText;
    }
    if (builder.contentView) {
        self.alertDialogView.contentView = builder.contentView;
    }
    if (builder.positiveBtnTitle) {
        [self.alertDialogView.positiveBtn setTitle:builder.positiveBtnTitle forState:UIControlStateNormal];
    }
    if (builder.cancelBtnTitle) {
        [self.alertDialogView.cancelBtn setTitle:builder.cancelBtnTitle forState:UIControlStateNormal];
    }
}

// 积极的
- (void)positiveBtnTap {
    if (self.builder.positiveBtnBlock) {
        self.builder.positiveBtnBlock();
    }
    [self hide];
}
// 消极的
- (void)cancelBtnTap {
    if (self.builder.cancelBtnBlock) {
        self.builder.cancelBtnBlock();
    }
    [self hide];
}


#pragma mark - getter and setter
- (AlertDialogView *)alertDialogView {
    if (!_alertDialogView) {
        _alertDialogView = [[AlertDialogView alloc] initWithAlertDialogBuilder:self.builder];
        [_alertDialogView.positiveBtn addTarget:self action:@selector(positiveBtnTap) forControlEvents:UIControlEventTouchUpInside];
        [_alertDialogView.cancelBtn addTarget:self action:@selector(cancelBtnTap) forControlEvents:UIControlEventTouchUpInside];

    }
    return _alertDialogView;
}

@end

@implementation AlertDialogBuilder

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.buildTitleBackgroundColor([UIColor whiteColor])
        .buildTitleFontColor([UIColor textBlackColor])
        .buildTitleFontSize(20.0)
        .buildTextAlignment(NSTextAlignmentCenter);
    }
    return self;
}

- (AlertDialogBuilder *(^)(UIColor *titleBackgroundColor))buildTitleBackgroundColor {
    return ^(UIColor *titleBackgroundColor) {
        self.titleBackgroundColor = titleBackgroundColor;
        return self;
    };
}
- (AlertDialogBuilder *(^)(UIColor *titleFontColor))buildTitleFontColor {
    return ^(UIColor *titleFontColor) {
        self.titleFontColor = titleFontColor;
        return self;
    };
}
- (AlertDialogBuilder *(^)(CGFloat titleFontSize))buildTitleFontSize {
    return ^(CGFloat titleFontSize) {
        self.titleFontSize = titleFontSize;
        return self;
    };
}
- (AlertDialogBuilder *(^)(NSString *title))buildTitle {
    return ^(NSString *title) {
        self.title = title;
        return self;
    };
}
- (AlertDialogBuilder *(^)(NSString *contentText))buildContentText {
    return ^(NSString *contentText) {
        self.contentText = contentText;
        return self;
    };
}
- (AlertDialogBuilder *(^)(NSTextAlignment textAlignment))buildTextAlignment {
    return ^(NSTextAlignment textAlignment) {
        self.textAlignment = textAlignment;
        return self;
    };
}
- (AlertDialogBuilder *(^)(UIView *contentView))buildContentView {
    return ^(UIView *contentView) {
        self.contentView = contentView;
        return self;
    };
}

- (AlertDialogBuilder *(^)(NSString *cancelBtnTitle))buildCancelBtnTitle {
    return ^(NSString *cancelBtnTitle) {
        self.cancelBtnTitle = cancelBtnTitle;
        return self;
    };
}
- (AlertDialogBuilder *(^)(NSString *positiveBtnTitle))buildPositiveBtnTitle {
    return ^(NSString *positiveBtnTitle) {
        self.positiveBtnTitle = positiveBtnTitle;
        return self;
    };
}

- (AlertDialogBuilder *(^)(void(^cancelBtnBlock)(void)))buildCancelBtnBlockBlock {
    return ^(void(^cancelBtnBlock)(void)) {
        self.cancelBtnBlock = cancelBtnBlock;
        return self;
    };
}
- (AlertDialogBuilder *(^)(void(^positiveBtnBlock)(void)))buildPositiveBtnBlock {
    return ^(void(^positiveBtnBlock)(void)) {
        self.positiveBtnBlock = positiveBtnBlock;
        return self;
    };
}

- (AlertDialog *)buildAlertDialog {
    AlertDialog *alertDialog = [[AlertDialog alloc] initWithAlertDialogBuilder:self];
    
    return alertDialog;
}

@end
